# 王磊的作业

import random

# 银行名称
bank_gong = "中国工商银行 "
bank_nong = '中国农业银行 '

# 用户信息字典
bankgong = {}
banknong = {}


# 银行选择
def xuan():
    return (
        "*******************************\n*      欢迎使用多行操作系统       *\n*******************************\n*      1、%s          *\n*      2、%s          *\n*         3、bye              *\n*******************************")


# 精美的界面
def banks():
    return (
        "*******************************\n*        %s           *\n*******************************\n*         1、开户              *\n*         2、存钱              *\n*         3、取钱              *\n*         4、转账              *\n*         5、查询              *\n*         6、bye              *\n*******************************")


def xuanbank():
    print(xuan() % (bank_gong, bank_nong))
    xyh = input("请选择银行>>>>>")
    if xyh == '1':
        return 1
    elif xyh == '2':
        return 2
    elif xyh == '3':
        return 3
    else:
        return 4


# 开户
def user_info(x, y):
    account = random.randint(10000000, 99999999)
    username = input("请输入您的姓名：")
    while True:
        password = input("请输入您的密码：")
        if password.isdigit():
            if 99999 < int(password) < 1000000:
                break
            else:
                print('输入的密码需为六位数！')
        else:
            print('请输入纯数字密码！')
    country = input("\t\t请输入您的国家：")
    province = input("\t\t请输入您的省份：")
    street = input("\t\t请输入您的街道：")
    door = input("\t\t请输入您的门牌号：")
    n = bank_add(account, username, password, country, province, street, door, x, y)
    if n == 1:
        print("开户成功，以下是您的详细信息")
        info = '''
        --------%s--------
        账号:%s
        姓名:%s
        密码:******
        国家:%s
        省份:%s
        街道:%s
        门牌号:%s
        余额:%s
        '''
        print(info % (x, account, username, country, province, street, door, y[account]["money"]))
    elif n == 2:
        print("账户已存在")
    elif n == 3:
        print("数据库已满")


def bank_add(account, username, password, country, province, street, door, x, y):
    if len(y) >= 100:
        return 3
    elif account in y:
        return 2
    else:
        y[account] = {
            "username": username,
            "password": password,
            "country": country,
            "province": province,
            "street": street,
            "door": door,
            "money": 0,
            "bankname": x
        }
        return 1


# 存款
def saving(y):
    hao = int(input("请输入您的账号："))
    money = int(input('请输入存入的金额：'))
    if hao in y:
        y[hao]['money'] += money
        return True
    else:
        return False


def cun(y):
    c = saving(y)
    if c == True:
        print('存款成功!')
    else:
        print('存款失败！')


# 取钱
def Withdrawal(y):
    hao = int(input("请输入您的账号："))
    password = input("请输入您的密码：")
    money = int(input('请输入取出的金额：'))
    if hao not in y:
        return 1
    elif password != y[hao]['password']:
        return 2
    elif money <= y[hao]['money']:
        y[hao]['money'] -= money
        return 0
    else:
        return 3


def qu(y):
    q = Withdrawal(y)
    if q == 0:
        print('取款成功！')
    elif q == 1:
        print('输入的账号不存在！')
    elif q == 2:
        print('输入的密码有误！')
    else:
        print('你没钱！')


# 转账
def Transfer(y, z):
    fhao = int(input("请输入转出的账号："))
    shao = int(input("请输入转入的账号："))
    password = input("请输入转出账号的密码：")
    money = int(input('请输入转出的金额：'))
    if fhao not in y:
        return 1
    elif shao not in y and shao not in z:
        return 1
    elif password != y[fhao]['password']:
        return 2
    elif money <= y[fhao]['money']:
        y[fhao]['money'] -= money
        if shao in y:
            y[shao]['money'] += money
        else:
            z[shao]['money'] += money
        return 0
    else:
        return 3



def zhuan(y, z):
    q = Transfer(y, z)
    if q == 0:
        print('转账成功！')
    elif q == 1:
        print('转出或转入账号不存在！')
    elif q == 2:
        print('转出账号密码有误！')
    else:
        print('你没钱！')


# 查询
def Inquire(x, y):
    hao = int(input("请输入查询的账号："))
    password = input("请输入账号的密码：")
    if hao not in y:
        print('该用户不存在！')
    elif password != y[hao]['password']:
        print('账号密码错误！')
    else:
        info = '''
        --------%s--------
        当前账号:%s
        姓名:%s
        密码:******
        余额:%s
        国家:%s
        省份:%s
        街道:%s
        门牌号:%s
        开户行:%s
        '''
        print(info % (
            x, hao, y[hao]['username'], y[hao]['money'], y[hao]['country'], y[hao]['province'],
            y[hao]['street'], y[hao]['door'], x))


# 程序主体2
def zhu(x, y, z):
    print(banks() % x)
    while True:
        index = input("请选择业务>>>>>")
        if index == "1":
            print("*************开户***************")
            user_info(x, y)
        elif index == "2":
            print("*************存钱***************")
            cun(y)
        elif index == '3':
            print('*************取钱***************')
            qu(y)
        elif index == '4':
            print('*************转账***************')
            zhuan(y, z)
        elif index == '5':
            print('*************查询***************')
            Inquire(x, y)
        elif index == '6':
            print('bye!')
            break
        else:
            print('您输入内容有误，请重新输入！')


# 程序主体
while True:
    xy = xuanbank()
    if xy == 1:
        zhu(bank_gong, bankgong, banknong)
    elif xy == 2:
        zhu(bank_nong, banknong, bankgong)
    elif xy == 3:
        print('bye!')
        break
    else:
        print('您输入内容有误，请重新输入！')
